//
//  CurrencyExchangeType.m
//  MarketDataColloctor
//
//  Created by jiro on 10/03/11.
//  Copyright 2010 jiro music. All rights reserved.
//

#import "CurrencyExchangeType.h"

#import "DbConstants.h"
#import "CurrencyExchangeTypeList.h"
#import "Nodes.h"
#import "../../Commons.h"

NSString* CurrencyExchangeType::tableName()
{
	return kText_CurrencyExchangeTypeTable;
}

NSString* CurrencyExchangeType::queryCreateTable()
{
	NSString* s = [ NSString stringWithFormat:
		@"create table %@ ( %@ %@, %@ %@, %@( %@, %@ ) );",
			CurrencyExchangeType::tableName(),
			kText_Code, kText_Text,
			kText_TargetCurrencyCode, kText_Text,
			kText_PrimaryKey, kText_Code, kText_TargetCurrencyCode
	];
	return s;
}

NSString* CurrencyExchangeType::querySelectAll( const std::vector< ClauseNode >& clauses )
{
	NSString* s = DbTableEntry::querySelectAll( CurrencyExchangeType::tableName(), clauses );
	return s;
}

NSString* CurrencyExchangeType::querySelect( const std::vector< FieldNode >& fieldNodes, const std::vector< ClauseNode >& clauses )
{
	NSString* s = DbTableEntry::querySelect( fieldNodes, CurrencyExchangeType::tableName(), clauses );
	return s;

}

NSString* CurrencyExchangeType::queryInsertInitialValues()
{
	struct CodePair { NSString* code; NSString* targetCode; };
	CodePair codePairs[] = {
		{ @"USD", @"JPY" },
		{ @"GBP", @"JPY" },
		{ @"INR", @"JPY" },
		{ @"IDR", @"JPY" },
		{ @"ECS", @"JPY" },
		{ @"EGP", @"JPY" },
		{ @"AUD", @"JPY" },
		{ @"CAD", @"JPY" },
		{ @"KRW", @"JPY" },
		{ @"KWD", @"JPY" },
		{ @"COP", @"JPY" },
		{ @"SAR", @"JPY" },
		{ @"SGD", @"JPY" },
		{ @"CHF", @"JPY" },
		{ @"SEK", @"JPY" },
		{ @"THB", @"JPY" },
		{ @"TWD", @"JPY" },
		{ @"CNY", @"JPY" },
		{ @"CLP", @"JPY" },
		{ @"DKK", @"JPY" },
		{ @"TRY", @"JPY" },
		{ @"JPY", @"JPY" },
		{ @"NZD", @"JPY" },
		{ @"NOK", @"JPY" },
		{ @"PYG", @"JPY" },
		{ @"PHP", @"JPY" },
		{ @"BRL", @"JPY" },
		{ @"VEF", @"JPY" },
		{ @"PEN", @"JPY" },
		{ @"HKD", @"JPY" },
		{ @"MYR", @"JPY" },
		{ @"ZAR", @"JPY" },
		{ @"MXN", @"JPY" },
		{ @"AED", @"JPY" },
		{ @"EUR", @"JPY" },
		{ @"JOD", @"JPY" },
		{ @"RON", @"JPY" },
		{ @"LBP", @"JPY" },
		{ @"RUB", @"JPY" },
		{ @"EUR", @"USD" },
		{ @"GBP", @"USD" },
		{ @"AUD", @"USD" },
		{ @"EUR", @"CHF" },
		{ @"GBP", @"CHF" },
		{ @"USD", @"CHF" },

	};

	NSString* s = [ NSString string ];
	for ( int i = 0, count = sizeof( codePairs ) / sizeof( CodePair ); i < count; i++ )
	{
		CurrencyExchangeType type;
		CodePair& codes = codePairs[ i ];
		type.code = codes.code;
		type.targetCurrencyCode = codes.targetCode;
		
		s = [ s stringByAppendingString: type.queryInsertValue() ];
		if ( i != count - 1 )
		{
			s = [ s stringByAppendingString: @", " ];
		}
	}
	return s;
}


CurrencyExchangeType::CurrencyExchangeType()
{}

CurrencyExchangeType::CurrencyExchangeType( std::vector< const char* > values )
{
	code				= [ NSString stringWithCString: values[ 0 ] encoding:NSUTF8StringEncoding ];
	targetCurrencyCode	= [ NSString stringWithCString: values[ 1 ] encoding:NSUTF8StringEncoding ];
}

NSString* CurrencyExchangeType::sqlInsertStatement() const
{
	return [ NSString stringWithFormat: @"insert into %@ values %@;", 
		CurrencyExchangeType::tableName(),
		queryInsertValue() ];
}

NSString* CurrencyExchangeType::queryInsertValue() const
{
	NSString* s = [ NSString stringWithFormat: @"( '%@', '%@' )", code, targetCurrencyCode ];
	return s;
}

NSString* CurrencyExchangeType::queryInsert() const
{
	NSString* s = [ NSString stringWithFormat:
		@"insert into %@ values %@;",
		tableName(), queryInsertValue() ];
	return s;
}

NSString* CurrencyExchangeType::toString() const
{
	return [ NSString stringWithFormat: @"%@/%@", code, targetCurrencyCode ];
}
